#include <iostream>
#include "tcp_serve.hpp"
#include "socket.hpp"
#include "Http.hpp"
#include "log.hpp"
void Usage(std::string argu)
{
    std::cout << "Usage: " << "server_port" << std::endl;
}
std::shared_ptr<http_response> login(std::shared_ptr<http_request> req)
{
    LOG(DEBUG, "================\n");
    std::string userdata;
    if (req->method() == "GET")
    {
        userdata += req->args();
    }
    else if (req->method() == "POST")
    {
        userdata += req->text();
    }
    else
    {
    }
    LOG(DEBUG, "enter data handler,data is : %s\n", userdata.c_str());
    auto response = factory::build_response_ptr();
    response->add_status_line(200, "OK");
    response->add_header("Content-Tyep", "text/html");
    response->add_text("<html><h1>handler data done</h1></html>");
    LOG(DEBUG,"==================\n");
    return response;
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }
    uint16_t port = std::stoi(argv[1]);
    http_server _http_server;
    //仿照路径，进行功能路由
    _http_server.add_handler("/login", login);
    // 本质：就是将&http_server::handler_http_reqeust函数和_http_service()函数关联起来，并传递参数
    tcp_server tcp_ptr(port, bind(&http_server::handler_http_reqeust, &_http_server, std::placeholders::_1));
    tcp_ptr.loop();
    return 0;
}